package com.king.framework.base.domain.vo;

import java.util.Collection;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import com.alibaba.fastjson2.annotation.JSONField;
import com.king.common.constant.Constants;
import com.king.modules.system.domain.SysUserEntity;

/**
 * 登录用户身份权限
 * 
 * @author king
 */
public class LoginUser implements UserDetails {
	private static final long serialVersionUID = 1L;

	/**
	 * 用户ID
	 */
	private Long userId;
	
	/**
	 * 集团id
	 */
	private Long groupid;

	/**
	 * 部门ID
	 */
	private Long orgid;
	

	private String orgName;

	/**
	 * 用户唯一标识
	 */
	private String token;

	/**
	 * 登录时间
	 */
	private Long loginTime;

	/**
	 * 过期时间
	 */
	private Long expireTime;

	/**
	 * 登录IP地址
	 */
	private String ipaddr;

	/**
	 * 登录地点
	 */
	private String loginLocation;

	/**
	 * 浏览器类型
	 */
	private String browser;

	/**
	 * 操作系统
	 */
	private String os;

	/**
	 * 权限列表
	 */
	private Set<String> permissions;

	/**
	 * 用户信息
	 */
	private SysUserEntity user;

	// 存储SpringSecurity所需要的权限信息的集合
	@JSONField(serialize = false)
	// 由于redis的原因,该类型不能序列化
	private Set<GrantedAuthority> authorities;
	
	
	public boolean isAdmin() {
		return isAdmin(getUserId());
	}
	
	public static boolean isAdmin(Long userId) {
		return userId != null && Constants.USER_ADMIN_USERID == userId;
	}

	public Long getUserId() {
		return userId;
	}

	public void setUserId(Long userId) {
		this.userId = userId;
	}

	public Long getOrgid() {
		return orgid;
	}

	public void setOrgid(Long orgid) {
		this.orgid = orgid;
	}

	public String getToken() {
		return token;
	}

	public void setToken(String token) {
		this.token = token;
	}

	public LoginUser() {
	}

//	public LoginUser(SysUser user, Set<String> permissions) {
//		this.user = user;
//		this.permissions = permissions;
//	}

	public LoginUser(Long userId,Long groupid, Long orgid,String orgName, SysUserEntity user, Set<String> permissions) {
		this.userId = userId;
		this.groupid = groupid;
		this.orgid = orgid;
		this.orgName = orgName;
		this.user = user;
		this.permissions = permissions;
	}

	@JSONField(serialize = false)
	@Override
	public String getPassword() {
		return user.getPassword();
	}

	@Override
	public String getUsername() {
		return user.getUserName();
	}

	/**
	 * 账户是否未过期,过期无法验证
	 */
	@JSONField(serialize = false)
	@Override
	public boolean isAccountNonExpired() {
		return true;
	}

	/**
	 * 指定用户是否解锁,锁定的用户无法进行身份验证
	 * 
	 * @return
	 */
	@JSONField(serialize = false)
	@Override
	public boolean isAccountNonLocked() {
		return true;
	}

	/**
	 * 指示是否已过期的用户的凭据(密码),过期的凭据防止认证
	 * 
	 * @return
	 */
	@JSONField(serialize = false)
	@Override
	public boolean isCredentialsNonExpired() {
		return true;
	}

	/**
	 * 是否可用 ,禁用的用户不能身份验证
	 * 
	 * @return
	 */
	@JSONField(serialize = false)
	@Override
	public boolean isEnabled() {
		return true;
	}

	public Long getLoginTime() {
		return loginTime;
	}

	public void setLoginTime(Long loginTime) {
		this.loginTime = loginTime;
	}

	public String getIpaddr() {
		return ipaddr;
	}

	public void setIpaddr(String ipaddr) {
		this.ipaddr = ipaddr;
	}

	public String getLoginLocation() {
		return loginLocation;
	}

	public void setLoginLocation(String loginLocation) {
		this.loginLocation = loginLocation;
	}

	public String getBrowser() {
		return browser;
	}

	public void setBrowser(String browser) {
		this.browser = browser;
	}

	public String getOs() {
		return os;
	}

	public void setOs(String os) {
		this.os = os;
	}

	public Long getExpireTime() {
		return expireTime;
	}

	public void setExpireTime(Long expireTime) {
		this.expireTime = expireTime;
	}

	public Set<String> getPermissions() {
		return permissions;
	}

	public void setPermissions(Set<String> permissions) {
		this.permissions = permissions;
	}

	public SysUserEntity getUser() {
		return user;
	}

	public void setUser(SysUserEntity user) {
		this.user = user;
	}

	public Long getGroupid() {
		return groupid;
	}

	public void setGroupid(Long groupid) {
		this.groupid = groupid;
	}
	

	public String getOrgName() {
		return orgName;
	}

	public void setOrgName(String orgName) {
		this.orgName = orgName;
	}

	@Override
	public Collection<? extends GrantedAuthority> getAuthorities() {
		if (Objects.nonNull(authorities)) {
			return authorities;
		}
		// 把permissions中字符串类型的权限信息转换成GrantedAuthority对象存入authorities中
		authorities = permissions.stream().map(SimpleGrantedAuthority::new).collect(Collectors.toSet());
		return authorities;
	}
}
